# -- coding: utf-8 --
import sys
from scrapy.spider import BaseSpider
from scrapy.http import Request
from scrapy.selector import HtmlXPathSelector 
from scrapy.contrib.exporter import XmlItemExporter
from scrapy.contrib.exporter import JsonItemExporter
from scrapy.contrib.exporter import JsonLinesItemExporter
from scrapy.contrib.exporter import CsvItemExporter

from tutorial.items import AudioLibCategory

class TvoxAudioLibCategorySpider(BaseSpider):
    name = "tvoxaudiolibcat"
    allowed_domains = ["www.thereminvox.com"]
    start_urls = [
        "http://www.thereminvox.com/filemanager/list/2/index.html",
        "http://www.thereminvox.com/filemanager/list/5/index.html"
    ]

    def parse(self, response):
    	sel = HtmlXPathSelector(response)
	items = []
	###### titolo
	item = AudioLibCategory()
        titles = sel.select("//div[@class='topicPageTitle']/text()")
	item['title'] = titles[0].extract()
	print "Titolo: %s" % item['title']
	items.append(item)

	###### abstract
        abstracts = sel.select("//div[@class='topicAbstract']/text()")
	items[0]['abstract'] = abstracts[0].extract()
	print "Sommario: %s" % items[0]['abstract']

       	file = open('categories.json', 'a')
       	exporter = JsonLinesItemExporter(file)
       	exporter.start_exporting()

	for item in items:
       		exporter.export_item(item)

       	exporter.finish_exporting()
